package com.liu.project.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.project.aspect.WebLogAspect;
import com.liu.project.pojo.Feedback;
import com.liu.project.pojo.FeedbackType;
import com.liu.project.service.FeedbackService;
import com.liu.project.utils.Message;
import com.sun.javafx.font.freetype.FTFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: project
 * @description: 反馈控制
 * @author: 86187
 * @create: 2022-02-21 16:38
 **/

@Api(tags = "反馈信息",description = "反馈的获取、展示、修改、增加;数据分页查询")
@Controller
@RequestMapping("/api/feedback")
public class FeedbackController {

    @Autowired
    FeedbackService feedbackService;

    @PreAuthorize("hasAuthority('ADMIN')")
    @ApiOperation("获取反馈情况(ADMIN权限)")
    @ApiResponse(code = 200,message = "请求成功",response = Message.class)
    @PostMapping("/findAll")
    @ResponseBody
    public Message<PageInfo<Feedback>> findAll(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Feedback> all = feedbackService.findAll();
        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(all);
        return new Message<PageInfo<Feedback>>(200,"请求成功",feedbackPageInfo);
    }

    @ApiOperation("获取所有反馈类型")
    @PostMapping("/findAllType")
    @ApiResponse(code = 200,message = "请求成功",response = Message.class)
    @ResponseBody
    public Message< PageInfo<FeedbackType>> findAllType(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<FeedbackType> allType = feedbackService.findAllType();
        PageInfo<FeedbackType> feedbackPageInfo = new PageInfo<FeedbackType>(allType);
        return new Message< PageInfo<FeedbackType>>(200, "请求成功",feedbackPageInfo);
    }

    @ApiOperation("fid获取反馈类型")
    @ApiResponse(code = 200,message = "请求成功",response = Message.class)
    @PostMapping("/findTypeByFid/{fid}")
    @ResponseBody
    public Message<String> findTypeByFid(@PathVariable("fid") int fid){
        return new Message<String>(200,"请求成功",feedbackService.findTypeByFid(fid));
    }


    @ApiOperation("id获取反馈情况")
    @ApiResponse(code = 200,message = "请求成功",response = Message.class)
    @PostMapping("/findOneById/{id}")
    @ResponseBody
    public Message<Feedback> findOneById(@PathVariable("id") int id){
            return new Message<Feedback>(200,"请求成功",feedbackService.findOneById(id));
    }

    @ApiOperation("反馈类型id获取反馈情况")
    @ApiResponses({ @ApiResponse(code = 200,message = "请求成功",response = Message.class),
            @ApiResponse(code = 0,message = "数据不存在",response = Message.class)})
    @PostMapping("/findByType/{fid}")
    @ResponseBody
    public Message<List<Feedback>> findByType(@PathVariable("fid") int fid){
        if (feedbackService.findByType(fid)!=null){
            return new Message<List<Feedback>>(200,"请求成功",feedbackService.findByType(fid));
        }else  return new Message<List<Feedback>>(0,"暂无数据");

    }

    @ApiOperation("treatment获取反馈情况")
    @ApiResponses({ @ApiResponse(code = 200,message = "请求成功",response = Message.class),
            @ApiResponse(code = 0,message = "数据不存在",response = Message.class)})
    @PostMapping("findByTreatment{treatment}")
    @ResponseBody
    public Message<List<Feedback>> findByTreatment(@PathVariable("treatment") boolean treatment){
        if (feedbackService.findByTreatment((treatment)) !=null){
            return new Message<List<Feedback>>(200, "请求成功", feedbackService.findByTreatment(treatment));
        }
        else  return new Message<List<Feedback>>(0,"暂无数据");
    }

    @ApiOperation("添加反馈数据")
    @ApiResponses({ @ApiResponse(code = 200,message = "请求成功",response = Message.class),
            @ApiResponse(code = 400,message = "请求失败",response = Message.class),
            @ApiResponse(code = 0,message = "请勿重复提交",response = Message.class)})
    @PostMapping("/add1")
    @ResponseBody
    public Message<Void> add1(Feedback feedback) {
        if (feedbackService.findOneById(feedback.getId())==null) {
            System.out.println(feedbackService.findOneById(feedback.getId()));
            if (feedbackService.insertOne1(feedback)) {
                return new Message<>(200, "请求完成");
            } else return new Message<>(400, "请求失败");
        }else return new Message<>(0, "请勿重复提交");
    }


    @ApiOperation("添加反馈类型数据")
    @PostMapping("/add2")
    @ApiResponses({ @ApiResponse(code = 200,message = "请求成功",response = Message.class),
            @ApiResponse(code = 400,message = "请求失败",response = Message.class)})
    @ResponseBody
    public Message<Void> add2(FeedbackType feedbackType){
        if (feedbackService.insertOne2(feedbackType)){
            return new Message<>(200,"请求完成");
        }else
            return new Message<>(400, "请求失败");
    }


    @ApiOperation("title获取反馈情况")
    @ApiResponses({ @ApiResponse(code = 200,message = "请求成功",response = Message.class),
            @ApiResponse(code = 0,message = "暂无数据",response = Message.class)})
    @PostMapping("/findOneByTitle/{key}")
    @ResponseBody
    public Message<List<Feedback>> findOneByTitle(@PathVariable("key") String key){
        if (feedbackService.findByKey(key)!=null){
            return new Message<List<Feedback>>(200,"请求成功",feedbackService.findByKey(key));
        }else  return new Message<List<Feedback>>(0,"暂无数据");

    }


    @ApiOperation("修改反馈数据")
    @ApiResponses({ @ApiResponse(code = 200,message = "请求成功",response = Message.class),
            @ApiResponse(code = 400,message = "请求失败",response = Message.class)})
    @PostMapping("/update")
    @ResponseBody
    public Message<Void> update(Feedback feedback){
        int id = feedback.getId();
        if (feedbackService.updateOne(feedback)){
            return new Message<>(200,"请求完成");
        }else
            return new Message<>(400, "请求失败");
    }


    @ApiOperation("删除反馈数据")
    @ApiResponses({ @ApiResponse(code = 200,message = "请求成功",response = Message.class),
            @ApiResponse(code = 400,message = "请求失败",response = Message.class)})
    @PostMapping("/deleteOne/{id}")
    @ResponseBody
    public Message<Void> deleteOne(@PathVariable("id") int id){
        boolean b = feedbackService.deleteOne(id);
        if (b){
            return new Message<>(200,"请求完成");
        }else
            return new Message<>(400, "请求失败");
    }



}
